package com.doremi.service.play_songs;

import java.io.IOException;

import android.content.ContentUris;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;

//<uses-permission android:name="android.permission.WAKE_LOCK" />
// si se quiere que siga reproduciendo cuando el telefono
//se pone en modo de ahorro poner la sentencia en el manifiest.xml
/*
 * esta clase intenta simplificar el uso 
 * de los MediaPlayer
 * se le pasa un MediaPlayer junto con un track y lo reproduce (play)
 * o lo deja de reproducir  (stop)
 */
public class PlaySongs {
	
	
	
	static public void play(MediaPlayer mediaPlayer,long trackId,Context context)
	{
				
		prepareMediaPlayer(mediaPlayer,trackId, context);
		mediaPlayer.start();
	}
	
	static private void prepareMediaPlayer(MediaPlayer mediaPlayer,long trackId,Context context)
	{
		
		mediaPlayer.reset();
		
		Uri contentUri = ContentUris.withAppendedId(	android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,trackId);
		mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
		try {
			mediaPlayer.setDataSource(context, contentUri);
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			mediaPlayer.prepare();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
	
	static public void stop(MediaPlayer mediaPlayer)
	{
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.stop();
	
		}
		
	
		
	}
	
	static public void pause(MediaPlayer mediaPlayer)
	{
		if (mediaPlayer.isPlaying()) {
			mediaPlayer.pause();
	
		}
		
	
		
	}
	
	static public void unPause(MediaPlayer mediaPlayer)
	{
		if (!mediaPlayer.isPlaying()) {
			mediaPlayer.start();
	
		}
	
	}
	static public void foward(MediaPlayer mediaPlayer)
	{
		if (mediaPlayer.isPlaying()) {
		  int pos=mediaPlayer.getCurrentPosition();
		  mediaPlayer.seekTo(pos+2500);
		}
		
	}

	public static void backward(MediaPlayer mediaPlayer) {
		if (mediaPlayer.isPlaying()) {
			  int pos=mediaPlayer.getCurrentPosition();
			  mediaPlayer.seekTo(pos-2500);
			}
		
	}


}
